<?php

require_once('Zend/Db/Table/Row/Abstract.php');
require_once('Zend/Db/Table.php');
require_once('Zend/Locale.php');

class Core_Locale
extends Zend_Db_Table_Row_Abstract
{
	/**
	 * Name of the namespace in which we're storing various global locale data.
	 *
	 * @var string
	 */
	const LOCALE_NAMESPACE = 'CORE_LOCALE_NAMESPACE';


	/**
	 * Private static reference to the user table.
	 *
	 * @return Zend_Db_Table
	 */
	private static $table;

	/**
	 * Name of the class of the Zend_Db_Table_Abstract object.
	 *
	 * @var string
	 */
	protected $_tableClass = 'Zend_Db_Table';

	/**
	 * Select access method, for slightly more complex queries.
	 *
	 * @return Zend_Db_Table
	 */
	public static function getTableInstance()
	{
		if ( empty(Core_Locale::$table ) )
		{
			Core_Locale::$table = new Zend_Db_Table('locale');
			Core_Locale::$table->setRowClass('Core_Locale');
		}
			
		return Core_Locale::$table;
	}

	/**
	 * Factory Method. Spits out a locale object that's tied to the database.
	 *
	 * @param $idOrIsoCode Unique DB Identifier or fully qualified ISO Code.
	 * @return Core_Locale
	 */
	public static function factory( $id_or_iso  = null)
	{
		$table = Core_Locale::getTableInstance();
			
		if ( is_null($id_or_iso) )
		{
			$row = $table->createRow(array(), Zend_Db_Table_Abstract::DEFAULT_DB);
		}
		else if ( is_int($id_or_iso) )
		{
			$row = $table->fetchRow( $table->select()->where('localeId=?',$id_or_iso));
		}
		else
		{
			// Slice the ISO code into segments.
			$language = Locale::getPrimaryLanguage ($id_or_iso);
			$country =  Locale::getRegion ($id_or_iso);

			// Apply them to the search string.
			$select = $table->select();
			$select->where('country=?', $country);
			$select->where('language=?', $language);

			$row = $table->fetchRow( $select );
		}
			
		return $row;
	}

	/**
	 * Retrieves the given application locale, or autosets it from autodetected values.
	 * If autodetected values are not available or are not supported, the default language
	 * is used.
	 *
	 * @return Core_Locale
	 */
	public static function getApplicationLocale ( )
	{
		$session = new Zend_Session_Namespace( self::LOCALE_NAMESPACE );

		if ( empty($session->locale) )
		{
			$session->locale = self::detectApplicationLocale();
		}

		// Make sure we're connected to a table.
		if ( !$session->locale->isConnected() )
		{
			$table = self::getTableInstance();
			$session->locale->setTable($table);
		}

		return $session->locale;
	}

	/**
	 * Auto detection of the application locale.
	 */
	private static function detectApplicationLocale ()
	{
		// Store the new locale
		self::setApplicationLocale( $locale );
		return $localeRow;
	}

	/**
	 * Sets the application locale to the given location string, assuming
	 * it is a valid locale
	 *
	 * @param Zend_Locale $locale
	 */
	public static function setApplicationLocale ( $locale )
	{
		// Retrieve the locale.
		$row = self::factory( $locale->toString() );

		if ( !empty($row) )
		{
			$session = new Zend_Session_Namespace( self::LOCALE_NAMESPACE );
			$session->locale = $row;
		}
	}

	/**
	 * Formats the locale to the ISO code.
	 */
	public function toIsoCode()
	{
		return Locale::composeLocale(array(
		    'language' => $this->language,
		    'region'  => $this->country));
	}
}